let timeCountFlag;
let timeCount;
let chessNum = {
	"green": 0,
	"yellow": 0,
	"red": 0,
	"blue": 0
}
let chessBlockNum = {
	"green": 89,
	"yellow": 89,
	"red": 89,
	"blue": 89
}
var isMyTurn = false;


function startTimeCount() {
	timeCount = 299;
	timeCountFlag = setInterval(function(){
		if(timeCount >= 0) {
			$(".time_count").text(timeCount+" S");
			timeCount -= 1;
		} else {
			clearInterval(timeCountFlag);
			sendLoseMsg();
		}
	},1000);
}

function gameError() {
	clearInterval(timeCountFlag);
	$(".pass_btn").hide();
	$(".confirm_btn").hide();
	isMyTurn = false;
}

function gameStart() {
	$(".loading-box").hide();
	$("#indexVue").show();
	createChess = createChess(chessColor);
	myAlert("success","匹配成功，游戏开始！");
	let indexVue = new Vue({
		el: '#indexVue',
		data: {
			allChess: createChess
		}
	});
	if(chessColor === "yellow") {
		myTurn();
	} else {
		otherTurn();
	}
}

function gameRunning(message) {
	let chessId = message.chess;
	chessId = chessId.substr(0,chessId.indexOf("_down"));
	let chessMsg = createChess[chessId].css;
	chessMsg = chessMsg.substr(0,chessMsg.indexOf(chessColor))
				+ message.chessColor + chessMsg.substr(chessMsg.indexOf(chessColor)+chessColor.length);
	chessMsg = chessMsg.substr(0,chessMsg.indexOf("margin-top"));
	chessMsg += "left: " + (message.line*35+21) + "px;top: " + (message.row*35+21) + "px;";

	let style = "transform-origin: 0% 0%; transform: rotate(" + message.nowChessShape + "deg)";
	if(message.levelChange && message.uprightChange) {
		style += " scale(-1,-1);";
	} else if(message.levelChange) {
		style += " scale(-1,1);";
	} else if(message.uprightChange) {
		style += " scale(1,-1);";
	} else {
		style += ";";
	}

	let addChess = "<div class='chess_down' style='"+ style + chessMsg +"'></div>";
	$(".chess_board").append(addChess);
	setOtherChessDown(message);
	let chessIndex = chessDownTurnEnum[message.chessColor];
	chessIndex += 1;
	if(chessIndex > 4) {
		chessIndex = 1;
	}
	if(chessColorEnum[chessIndex] === chessColor) {
		myTurn();
	} else {
		otherTurn();
	}
	chessNum[message.chessColor] = chessNum[message.chessColor] + 1;
	chessBlockNum[message.chessColor] = chessBlockNum[message.chessColor] - createChessDown[message.chess].number;
}

function setOtherChessDown(message) {
	let obj = dealShapeChangeWithParam(message);
	let x = message.row + obj.positionX;
	let y = message.line + obj.positionY;
	chessPosition[x][y] = -1;
	let sum = obj.left;
	for(let i = 1; i <= sum; i++) {
		chessPosition[x][y-i] = -1;
	}
	sum = obj.right;
	for(let i = 1; i <= sum; i++) {
		chessPosition[x][y+i] = -1;
	}
	sum = obj.top;
	for(let i = 1; i <= sum; i++) {
		chessPosition[x-i][y] = -1;
	}
	sum = obj.bottom;
	for(let i = 1; i <= sum; i++) {
		chessPosition[x+i][y] = -1;
	}
	sum = obj.left_top;
	if(sum === 1) {
		chessPosition[x-1][y-1] = -1;
	}
	sum = obj.left_bottom;
	if(sum === 1) {
		chessPosition[x+1][y-1] = -1;
	}
	sum = obj.right_bottom;
	if(sum === 1) {
		chessPosition[x+1][y+1] = -1;
	}
	sum = obj.right_top;
	if(sum === 1) {
		chessPosition[x-1][y+1] = -1;
	}
}

function myTurn() {
	isMyTurn = true;
	clearInterval(timeCountFlag);
	myAlert("success","当前你的回和，请落子！")
	$(".pass_btn").show();
	$(".confirm_btn").show();
	startTimeCount();
}

function otherTurn() {
	isMyTurn = false;
	clearInterval(timeCountFlag);
	myAlert("success","请等待对手落子！")
	$(".pass_btn").hide();
	$(".confirm_btn").hide();
	startTimeCount();
}

function positionConfirm(message) {
	sendMessage(message);
	otherTurn();
	chessNum[message.chessColor] = chessNum[message.chessColor] + 1;
	chessBlockNum[message.chessColor] = chessBlockNum[message.chessColor] - createChessDown[message.chess].number;
}

function gameEnd() {
	clearInterval(timeCountFlag);
	myAlert("warning","玩家认输，游戏结束！");
	let div = '<div class="gameResult-title"><span style="color: yellow;">黄色 </span>剩余 <span style="color: yellow;">'+(21-chessNum["yellow"])+'</span> 个棋子，剩余格数：<span style="color: yellow;">'+chessBlockNum["yellow"]+'</span></div>';
	$(".gameResult-box").append(div);
	div = '<div class="gameResult-title"><span style="color: blue;">蓝色 </span>剩余 <span style="color: blue;">'+(21-chessNum["blue"])+'</span> 个棋子，剩余格数：<span style="color: blue;">'+chessBlockNum["blue"]+'</span></div>';
	$(".gameResult-box").append(div);	
	div = '<div class="gameResult-title"><span style="color: red;">红色 </span>剩余 <span style="color: red;">'+(21-chessNum["red"])+'</span> 个棋子，剩余格数：<span style="color: red;">'+chessBlockNum["red"]+'</span></div>';
	$(".gameResult-box").append(div);
	div = '<div class="gameResult-title"><span style="color: green;">绿色 </span>剩余 <span style="color: green;">'+(21-chessNum["green"])+'</span> 个棋子，剩余格数：<span style="color: green;">'+chessBlockNum["green"]+'</span></div>';
	$(".gameResult-box").append(div);
	$(".gameOver-box").css("display","flex");
	$(".gameOver-box").show();
}